﻿@model Zatify.WebUI.Models.UserProfileCategoriesViewModel
@{
    ViewBag.Title = "Categories";
    ViewBag.ContentTitle = "Categories";
    Layout = "~/Views/Shared/_ContentLayout.cshtml";
}
<script type="text/javascript">

    var catsIds = [];

    $(document).ready(function () {

        if ($("#categoriesIds").val())
            catsIds = $("#categoriesIds").val().split(",");

        $("#btnSubmit").click(function () {

            $("#formCategories").submit();
        });
    });

    function addCategory(e, id) {

        var hiddenCategoriesIds = $("#categoriesIds");

        if ($(e).attr("checked")) {

            catsIds.push(id);

            if (catsIds.length == 1) {
                hiddenCategoriesIds.val(id);
            } else {
                hiddenCategoriesIds.val(catsIds.join(","));
            }
        }
        else {

            catsIds = jQuery.grep(catsIds, function (value) {
                return value != id;
            });

            hiddenCategoriesIds.val(catsIds.join(","));
        }
    }

    function addSingleCategory() {

        $.each($("input[type=radio]"), function (index, item) {

            addCategory(item, $(item).attr("id"));
        });
    }

</script>
<div class="user_registration_area">
    <div class="login_fillbox">
        @using (Html.BeginForm("Categories", "UserProfile", FormMethod.Post, new { id = "formCategories" }))
        {
            @Html.Hidden("categoriesIds", Model.SelectegCategoriesIds) 
        }
        @if (Model.CurrentCategory != null)
        {
            <div style="margin-top: 10px;" class="zatify_panel_btn">
                <a href="@Url.Action("Categories", "UserProfile", new { categoryId = "" })">
                    <input type="button" value="All Categories" class="btn apps_btn apps_gray_btn" name="" />
                </a>
                @Html.CategoryBreadCrumbs(Model.CurrentCategory, x => Url.Action("Categories", "UserProfile", new { categoryId = x }))
            </div>
        }
        <div class="categories_items">
            @foreach (var category in Model.Categories.OrderBy(c => c.SortOrder))
            {
                <div class="categories_line">
                    <label>
                        @if (category.ChildCategories.Count() > 0)
                        {
                            <a href="@Url.Action("Categories", "UserProfile", new { categoryId = category.CategoryId })">@category.CategoryName</a>
                        }
                        else
                        {
                            @category.CategoryName
                        }
                        @if (!category.IsSingleSelection)
                        {
                            if (category.IsSelectable())
                            {
                            <input class="categories_checkbox" onchange="addCategory(this, @category.CategoryId)" type="checkbox" 
                               @(Model.SelectedCategories.Any(c => c.CategoryId == category.CategoryId) ? "checked='checked' disabled='disabled'" : "") />
                            }
                        }
                        else
                        {
                            <input id="@(category.CategoryId)" class="categories_checkbox" type="radio" name="singleCategory" onclick="addSingleCategory()" 
                             @(Model.SelectedCategories.Any(c => c.CategoryId == category.CategoryId) ? "checked='checked'" : "") />
                        }
                    </label>
                </div>
            }
        </div>
        <div class="category_add_rem_btn">
            <input id="btnSubmit" type="submit" value="Done" class="blue_btn" />
        </div>
    </div>
    <div class="clearfix">
    </div>
</div>
